<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="UTF-8">
	<title>优惠券管理</title>
	<script th:src="@{/layui/layui.js}" type="text/javascript"></script>
	<link th:href="@{/layui/css/layui.css}" rel="stylesheet" />
</head>
<body>

<div class="layui-panel" style="padding: 8px;">
	<fieldset class="layui-elem-field layui-border-blue">
		<legend>搜索信息</legend>
		<form class="layui-form layui-form-pane" style="margin: 20px;">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">名称</label>
					<div class="layui-input-inline">
						<input class="layui-input" type="text" name="name" autocomplete="off" />
					</div>
				</div>
				<div class="layui-inline">
					<div class="layui-input-inline">
						<a class="layui-btn layui-btn-primary" lay-submit="" lay-filter="searchBtn">
							<i class="layui-icon layui-icon-search"></i>
							搜索
						</a>
					</div>
				</div>
			</div>
		</form>
	</fieldset>
	<div>
		<table id="couponTable" lay-filter="couponTable"></table>
	</div>
</div>
<script id="tool" type="text/html">
	<a class="layui-btn layui-btn-xs" lay-event="select">详情</a>
	<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
	<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="delete">删除</a>
</script>
<script id="tableBar" type="text/html">
	<a class="layui-btn layui-btn-normal layui-btn-md" lay-event="add">
		<i class="layui-icon layui-icon-add-1"></i>
		新增
	</a>
	<!--<a class="layui-btn layui-btn-danger layui-btn-md" lay-event="delete">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </a>-->
</script>
<script>
	var $;
	layui.use(['table','form','layer'],function(){
		var table = layui.table,form = layui.form,
				layer = layui.layer;
		$ = layui.jquery;

		//加载表格
		table.render({
			elem:'#couponTable',
			height:'full-160',
			page:true,
			toolbar:'#tableBar',
			url:'/system/cc/find',
			cols:[
				[
					{field:'id',title:'优惠券id',width:120,align: "center", sort: true},
					{field:'name',title:'优惠券名称',width:150,align: "center"},
					{field:'introduce',title:'优惠券介绍',align: "center"},
					{field:'label',title:'标签',width:100,align: "center"},
					{field:'minimumConsumption',title:'最低消费',width:100,align: 'center'},
					{field:'fullMinus',title:'满减金额',width:130,align: "center"},
					{field:'lim',title:'每人限领',width:100,align: "center"},
					{field:'useRange',title:'商品使用范围',width:150,align: "center"},
					{field:'type',title:'优惠券类型',width:100,align: "center",templet:function (data) {
										if (data.type == '0'){
											return '<span style="color: #F581B1;">注册领券</span>';
										}
										else if(data.type == '1'){
											return '<span style="color: #3f7dff;">通用领券</span>';
										}
										else if(data.type == '2'){
											return '<span style="color: #B5FFF8;"><a>兑换码</a><span>';
										}else{
											return data.type;
										}
						}},
					{field:'state',title:'状态',width:100,align: "center",templet:function (data) {
							if (data.state == '0'){
								return '<span style="color: #5FB878;">正常</span>';
							}else if(data.state == '1'){
								return '<span style="color: #FF5722;">已过期</span>';
							}
						}},
					{field:'number',title:'数量',width:100,align: "center"},
					{fixed:'right',title:'操作',width:300,align:'center',toolbar:'#tool'},
				]
			]
		})

		//监听搜索按钮 TODO 重写监听方法
		form.on('submit(searchBtn)',function(data){
			var dataForm = data.field;
			console.log(dataForm);
		})

		var active = {
			addOrUpdate:function (content) {
				var b = false;
				top.layer.open({
					type:2,
					content:content,
					area:['700px','650px'],
					btn:['确认保存','取消关闭'],
					btn1:function (i,o) {
						o.find('iframe')[0].contentWindow.submitBtn();
						b = true;
					},
					end:function () {
						if(b) layui.table.reload('couponTable');
					}
				})
			}
		}

		//表内工具
		table.on('toolbar(couponTable)',function (obj) {
			var event = obj.event;
			if("add" == event){
				active.addOrUpdate('/page/system/couponAdd');
			}
		})
		table.on('tool(couponTable)',function (obj) {
			var event = obj.event;
			if("edit" == event){
				active.addOrUpdate('/page/system/couponEdit/'+obj.data.id);
			}else if ("delete" == event){
				layer.msg('确定要删除这张券吗',{
					time:10000,//不做处理10秒后自动关闭
					btn: ['确定','取消'],
					btn1:function (i,o) {
						$.ajax({
							type:'post',
							url:'/system/cc/delete',
							data:obj.data,
							dataType:'json',
							success:function (result) {
								layer.msg(result.msg)
								if (result.is){
									layui.table.reload('couponTable');
								}
							}
						})
					}
				})
			}else if (event == 'select'){
				top.layer.open({
					type:2,
					content:"/page/system/couponDetails/"+obj.data.id,
					area:['900px','650px'],
				})
			}
		})

	});
</script>
</body>
</html>
